<?php
	include "../../public/commom/config.php";
	include "../lock2.php";
	include "../../public/commom/thumb.php";
	include "../../public/commom/commom.php";
	
	if($_POST){
		$name = $_POST["name"];
		
		$sql = "SELECT * FROM class WHERE name = '$name'";
		
		$result = mysqli_query($conn, $sql);
		
		$num = mysqli_num_rows($result);
		
		if(!$num) {
			/*图片*/
			$imgsrc = $_FILES["img"]["tmp_name"];//获取图片临时路径
			$fname = $_FILES["img"]["name"];//获取图片名称
			$a = explode(".", $fname);//获取图片后缀名
			$ext = array_pop($a);//
			$dst = $uploadPath . time().mt_rand(). "." . $ext;//随机生成图片名称
	
			//图片上传成功
			if(move_uploaded_file($imgsrc, $dst)) {
				thumb($dst, 200, 200);//生成缩略图
				$img = basename($dst);//获取带后缀名的图片名称
			}
			
			$sql1 = "INSERT INTO class (name, img) VALUES ('$name', '$img')";
			
			$result1 = mysqli_query($conn, $sql1);
			
			mysqli_close($conn);
				
			header("Location: ./index.php");
		} else {
			echo "分类已存在";
		}
	}
?>